package com.example.blog.modules.system.controller;

import com.example.blog.common.api.ApiResponse;
import com.example.blog.common.security.HasPermission;
import com.example.blog.modules.system.dto.SysConfigDTO;
import com.example.blog.modules.system.service.SysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "系统配置", description = "系统配置相关接口")
@RestController
@RequestMapping("/api/system/configs")
@RequiredArgsConstructor
public class SysConfigController {

    private final SysConfigService sysConfigService;

    @Operation(summary = "创建配置")
    @PostMapping
    @HasPermission("config:create")
    public ApiResponse<SysConfigDTO> createConfig(@Valid @RequestBody SysConfigDTO configDTO) {
        return ApiResponse.success(sysConfigService.createConfig(configDTO));
    }

    @Operation(summary = "更新配置")
    @PutMapping("/{id}")
    @HasPermission("config:update")
    public ApiResponse<SysConfigDTO> updateConfig(@PathVariable Long id, @Valid @RequestBody SysConfigDTO configDTO) {
        return ApiResponse.success(sysConfigService.updateConfig(id, configDTO));
    }

    @Operation(summary = "删除配置")
    @DeleteMapping("/{id}")
    @HasPermission("config:delete")
    public ApiResponse<Void> deleteConfig(@PathVariable Long id) {
        sysConfigService.deleteConfig(id);
        return ApiResponse.success();
    }

    @Operation(summary = "获取配置详情")
    @GetMapping("/{id}")
    @HasPermission("config:read")
    public ApiResponse<SysConfigDTO> getConfig(@PathVariable Long id) {
        return ApiResponse.success(sysConfigService.getConfig(id));
    }

    @Operation(summary = "获取配置值")
    @GetMapping("/value/{key}")
    @HasPermission("config:read")
    public ApiResponse<String> getConfigValue(@PathVariable String key) {
        return ApiResponse.success(sysConfigService.getConfigValue(key));
    }

    @Operation(summary = "批量获取配置值")
    @GetMapping("/values")
    @HasPermission("config:read")
    public ApiResponse<Map<String, String>> getConfigValues(@RequestParam List<String> keys) {
        return ApiResponse.success(sysConfigService.getConfigValues(keys));
    }

    @Operation(summary = "获取所有配置")
    @GetMapping("/all")
    @HasPermission("config:read")
    public ApiResponse<List<SysConfigDTO>> getAllConfigs() {
        return ApiResponse.success(sysConfigService.getAllConfigs());
    }

    @Operation(summary = "获取系统配置")
    @GetMapping("/system")
    @HasPermission("config:read")
    public ApiResponse<List<SysConfigDTO>> getSystemConfigs() {
        return ApiResponse.success(sysConfigService.getSystemConfigs());
    }

    @Operation(summary = "获取配置列表")
    @GetMapping
    @HasPermission("config:read")
    public ApiResponse<Page<SysConfigDTO>> getConfigs(
            @RequestParam(required = false) String keyword,
            Pageable pageable) {
        return ApiResponse.success(sysConfigService.getConfigs(keyword, pageable));
    }

    @Operation(summary = "刷新配置缓存")
    @PostMapping("/refresh")
    @HasPermission("config:refresh")
    public ApiResponse<Void> refreshCache() {
        sysConfigService.refreshCache();
        return ApiResponse.success();
    }
} 